package com.btpj.hospital_server.controller;

import cn.hutool.core.util.StrUtil;
import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.PowerAccountDTO;
import com.btpj.hospital_server.entity.PowerAccount;
import com.btpj.hospital_server.service.PowerAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理Controller
 *
 * @author BTPJ  2020/8/21
 */
@Api(value = "权限模块", tags = "账号接口")
@RestController
@RequestMapping("/account")
public class PowerAccountController {

    @Resource
    PowerAccountService powerAccountService;

    @ApiOperation(value = "登录", notes = "输入用户名和密码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping("/login")
    public CommonResult<PowerAccountDTO> login(@RequestParam String name, @RequestParam String password) {
        if (powerAccountService.loadUserByUsername(name) == null) {
            return CommonResult.error("用户不存在");
        }
        String jwtToken = powerAccountService.login(name, password);
        if (jwtToken == null) {
            return CommonResult.error("密码错误");
        } else {
            PowerAccount userInfo = powerAccountService.getUserInfo();
            PowerAccountDTO powerAccountDTO = PowerAccountDTO.entity2DTO(userInfo);
            powerAccountDTO.setToken(jwtToken);

            return CommonResult.success(powerAccountDTO, "登录成功");
        }
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/accountInfo")
    public CommonResult<PowerAccountDTO> getUserInfo() {
        PowerAccount userInfo = powerAccountService.getUserInfo();
        if (userInfo == null) {
            return CommonResult.expired("当前无登录的用户");
        }
        return CommonResult.success(PowerAccountDTO.entity2DTO(userInfo));
    }

    @ApiOperation(value = "账号退出", notes = "需要前端配合清除header中的Authority(Token)")
    @GetMapping("/logout")
    public CommonResult<Void> logout() {
        return CommonResult.success("用户注销成功");
    }

    @ApiOperation("更新账户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "原密码(为安全,需统一sh256加密)", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码(为安全,需统一sh256加密)", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "sureNewPassword", value = "确认新密码(为安全,需统一sh256加密)", paramType = "query", dataType = "String", required = true),
    })
    @PostMapping("/changePwd")
    public CommonResult<Void> changePwd(@RequestParam String password, @RequestParam String newPassword, @RequestParam String sureNewPassword) {
        if (powerAccountService.getUserInfo() == null) {
            return CommonResult.error("请先登录要修改密码的账户");
        }

        if (!powerAccountService.checkPassword(password)) {
            return CommonResult.error("原密码不正确");
        }

        if (StrUtil.isEmpty(newPassword)) {
            return CommonResult.error("密码不能设置为空");
        }

        if (!StrUtil.equals(newPassword, sureNewPassword)) {
            return CommonResult.error("新密码与确认密码不一致");
        }

        if (powerAccountService.changePwd(newPassword)) {
            return CommonResult.success("修改密码成功");
        } else {
            return CommonResult.error("修改密码出错");
        }
    }
}
